package com.jie.common.controller;


import com.jie.common.component.redis.CaptchaService;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @description:
 * @author: xiaojie
 * @since: 2025/1/3 0003
 */
@RestController
@RequestMapping("/captcha")
public class EasyCaptchaController {

    // 验证码缓存服务接口
    @Autowired
    private CaptchaService captchaService;

    @ApiOperation(value = "生成验证码")
    @RequestMapping("/get-captcha")
    public void getCaptchaImg(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        SpecCaptcha specCaptcha = new SpecCaptcha();
        specCaptcha.setLen(4);
        specCaptcha.setHeight(48);
        specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        String text = specCaptcha.text();
        session.setAttribute("verify_code",text);
        captchaService.saveImageCode("code",text);
        CaptchaUtil.out(specCaptcha,request,response);
    }

}
